package com.ruoyi.web.controller.vqms;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.vqms.domain.VQMSQueueReservation;
import com.ruoyi.vqms.service.IVQMSQueueReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 预约排队 控制器
 */
@RestController
@RequestMapping("/vqms/queue/reservation")
public class VQMSQueueReservationController extends BaseController {
    @Autowired
    private IVQMSQueueReservationService queueReservationService;

    /**
     * 查询预约排队列表
     */
    @PreAuthorize("@ss.hasPermi('vqms:queue:reservation:list')")
    @RequestMapping("/list")
    public TableDataInfo list(VQMSQueueReservation vqmsQueueReservation){
        startPage();
        List<VQMSQueueReservation> list=queueReservationService.selectList(vqmsQueueReservation);
        return getDataTable(list);
    }

    /**
     * 新增预约排队
     */
    @Log(title = "新增预约排队", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('vqms:queue:reservation:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VQMSQueueReservation vqmsQueueReservation){
        vqmsQueueReservation.setCreateBy(SecurityUtils.getUsername());
        queueReservationService.add(vqmsQueueReservation);
        return AjaxResult.success();
    }

    /**
     * 查询预约排队
     */
    @PreAuthorize("@ss.hasPermi('vqms:queue:reservation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(queueReservationService.getById(id));
    }

    /**
     * 修改预约排队
     */
    @Log(title = "修改预约排队", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('vqms:queue:reservation:update')")
    @PutMapping(value = "/update")
    public AjaxResult update(@RequestBody VQMSQueueReservation vqmsQueueReservation)
    {
        vqmsQueueReservation.setUpdateBy(SecurityUtils.getUsername());
        queueReservationService.update(vqmsQueueReservation);
        return AjaxResult.success();
    }

    /**
     * 删除预约排队                                                                                                                                                                                                                              
     */
    @Log(title = "删除预约排队", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('vqms:queue:reservation:delete')")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return AjaxResult.success(queueReservationService.delete(ids));
    }

    /**
     * 导出排队预约
     */
    @Log(title = "导出预约排队", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('vqms:queue:reservation:export')")
    @PostMapping("/export")
    public AjaxResult export(VQMSQueueReservation vqmsQueueReservation, HttpServletResponse response) {
        List<VQMSQueueReservation> list=queueReservationService.selectList(vqmsQueueReservation);
        ExcelUtil<VQMSQueueReservation> util = new ExcelUtil<>(VQMSQueueReservation.class);
        String filename = util.exportExcel(list, "排队预约").get("msg").toString();
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        try {
            if (!FileUtils.checkAllowDownload(downloadPath)) {
                return AjaxResult.error("资源文件("+filename+")非法，不允许下载。 ");
            }
            FileUtils.writeBytes(downloadPath, response.getOutputStream());
            return AjaxResult.success(filename);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
